package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.service.impl.ArticleReplyServiceImpl;
import com.example.forum.service.impl.ArticleServiceImpl;
import com.example.forum.utils.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/21
 */
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private ArticleReplyServiceImpl articleReplyService;

    @Resource
    private ArticleServiceImpl articleService;

    @Resource
    private RedisUtil redisUtil;
    
    private static final String REPLY_LIMIT_KEY = "reply_limit:";
    private static final int REPLY_WINDOW_SIZE = 60; // 1分钟窗口
    private static final int REPLY_LIMIT = 3; // 每分钟最多回复3次

    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("articleId") @NonNull Long articleId,
                            @RequestParam("content") @Validated String content) {
        //校验用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验帖子
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        String limitKey = REPLY_LIMIT_KEY + user.getId();
        
        // 检查是否被限流
        if (!redisUtil.isActionAllowed(limitKey, REPLY_WINDOW_SIZE, REPLY_LIMIT)) {
            return AppResult.failed("回复太频繁,请稍后再试");
        }

        //创建回帖
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReplyService.create(articleReply);

        return AppResult.success();
    }
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @RequestParam("articleId") @NonNull Long articleId) {
        //判断帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //查询回复
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        return AppResult.success(articleReplies);
    }
}
